package com.rainng.coursesystem.manager;

import com.rainng.coursesystem.dao.*;
import com.rainng.coursesystem.model.bo.AuthInfoBO;
import com.rainng.coursesystem.model.constant.UserType;
import com.rainng.coursesystem.model.entity.AdminEntity;
import com.rainng.coursesystem.model.entity.RoleEntity;
import com.rainng.coursesystem.model.entity.StudentEntity;
import com.rainng.coursesystem.model.entity.TeacherEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class UserManager extends BaseManager {


    private final AdminDAO adminDAO;
    private final TeacherDAO teacherDAO;
    private final StudentDAO studentDAO;
    private final RoleDAO roleDAO;
    private final UserGroupDAO userGroupDAO;

    public UserManager(AdminDAO adminDAO, TeacherDAO teacherDAO, StudentDAO studentDAO, RoleDAO roleDAO, UserGroupDAO userGroupDAO) {
        this.adminDAO = adminDAO;
        this.teacherDAO = teacherDAO;
        this.studentDAO = studentDAO;
        this.roleDAO = roleDAO;
        this.userGroupDAO = userGroupDAO;
    }

    public AuthInfoBO getAuthInfoByUsername(String username, Integer userType) {
        System.out.println(userType);
        if (userType == UserType.STUDENT) {
            return AuthInfoBO.fromStudent(studentDAO.getByNumber(username));
        } else if (userType == UserType.TEACHER) {
            return AuthInfoBO.fromTeacher(teacherDAO.getByNumber(username));
        } else if (userType == UserType.LEADER) {
            return AuthInfoBO.fromAdmin(adminDAO.getByUsername(username));
        } else if (userType == UserType.ADMIN) {
            return AuthInfoBO.fromAdmin(adminDAO.getByUsername(username));
        }
        else if (userType == UserType.DEVICE) {
        return AuthInfoBO.fromAdmin(adminDAO.getByUsername(username));
        }
        else if (userType == UserType.FIELD) {
            return AuthInfoBO.fromAdmin(adminDAO.getByUsername(username));
        }
        else if (userType == UserType.COMPANY) {
            return AuthInfoBO.fromAdmin(adminDAO.getByUsername(username));
        }
        return null;
    }
    //2023
    public AuthInfoBO getAuthInfoByUsernameWithoutUserType(String username) {

        log.info("进入权限验证");
        RoleEntity o = roleDAO.getByUsername(username);
        if(o == null){
            return null;
        }
        String role = o.getRole();
        //0公司 1 2 3 判断2 为主检
        Integer userType = Integer.valueOf(role);
        if (userType == UserType.STUDENT) {
            StudentEntity se = studentDAO.getByName(username);
            if(se == null){
                return null;
            }
            se.setPassword(o.getPassword());
            return AuthInfoBO.fromStudent(se);
        } else if (userType == UserType.TEACHER) {
            TeacherEntity te = teacherDAO.getByName(username);
            if(te == null){
                return null;
            }
            te.setPassword(o.getPassword());
            return AuthInfoBO.fromTeacher(te);
        } else if (userType == UserType. LEADER) {
            RoleEntity re = roleDAO.getByUsername(username);
            //AdminEntity ae = adminDAO.getByUsername(username);
            if(re == null){
                return null;
            }
            re.setPassword(o.getPassword());
            return AuthInfoBO.fromRole(re);
        }
        else if (userType == UserType.ADMIN) {
            AdminEntity ae = adminDAO.getByUsername(username);
            if(ae == null){
                return null;
            }
            ae.setPassword(o.getPassword());
            return AuthInfoBO.fromAdmin(ae);
        }
        else if (userType == UserType.DEVICE) {
            RoleEntity ae = roleDAO.getByUsername(username);
            if(ae == null){
                return null;
            }
            ae.setPassword(o.getPassword());
            AuthInfoBO bo = AuthInfoBO.fromRole(ae);
            bo.setUserType(UserType.DEVICE);
            return bo;
        }
        else if (userType == UserType.FIELD) {
            RoleEntity ae = roleDAO.getByUsername(username);
            if(ae == null){
                return null;
            }
            ae.setPassword(o.getPassword());
            AuthInfoBO bo = AuthInfoBO.fromRole(ae);
            bo.setUserType(UserType.FIELD);
            return bo;
        }
        else if (userType == UserType.TRIAL) {
            RoleEntity ae = roleDAO.getByUsername(username);
            if(ae == null){
                return null;
            }
            ae.setPassword(o.getPassword());
            AuthInfoBO bo = AuthInfoBO.fromRole(ae);
            bo.setUserType(UserType.TRIAL);
            return bo;
        }
        else if (userType == UserType.COMPANY) {
            RoleEntity ae = roleDAO.getByUsername(username);
            if(ae == null){
                return null;
            }
            ae.setPassword(o.getPassword());
            AuthInfoBO bo = AuthInfoBO.fromRole(ae);
            bo.setCompanyName(ae.getCompanyName());
            bo.setUserType(UserType.COMPANY);
            return bo;

        }
        return null;
    }



    public void updateStudentLastLoginTime(String number) {
        StudentEntity entity = studentDAO.getByNumber(number);
        if (entity == null) {
            return;
        }

        entity.setLastLoginTime(new Date());
        studentDAO.update(entity);
    }


    public String getDepartNameByUserName(String name){
        if(name == null){
            return null;
        }

        List<String> list = userGroupDAO.getDepartmentNameByUserName(name);
        String res = null;
        if(list.size() > 0){
            res = list.get(0);
        }
        return res;
    }

    public String getDepartIdByUserName(String name){
        if(name == null){
            return null;
        }

        List<String> list = userGroupDAO.getDepartmentIdByUserName(name);
        String res = null;
        if(list.size() > 0){
            res = list.get(0);
        }
        return res;
    }

    public String getUserIdByUserName(String name){
        if(name == null){
            return null;
        }

        List<String> list = roleDAO.getUserIdByUserName(name);
        String res = null;
        if(list.size() > 0){
            res = list.get(0);
        }
        return res;
    }



}
